﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PaymentHelper.Core.RelatedObject;
using PaymentHelper.Core.Extension;
using PaymentHelper.Core.ExceptionHandler;
using System.ComponentModel.DataAnnotations;

namespace PaymentHelper.Methods.PaypalDirect.Models
{
    public class ConfigModel : PaymentHelperModel
    {
        [Required(ErrorMessage = "API Username is required")]
        public string API_UserName { get; set; }

        [Required(ErrorMessage = "API Password is required")]
        public string API_Password { get; set; }

        [Required(ErrorMessage = "API Signature is required")]
        public string API_Signature { get; set; }

        public bool UseSandbox { get; set; }

        public override void InitModel(IList<PHSettingItem> paypalStandardSettings)
        {
            if (paypalStandardSettings == null || paypalStandardSettings.Count == 0)
                throw new PaymentHelperException(ExceptionLevel.Warning, "ConfigModel-InitModel", "List of settings is empty. Loading specified setting failed !");

            API_UserName = GetValueConfig("api_username", paypalStandardSettings);
            API_Password = GetValueConfig("api_userpass", paypalStandardSettings);
            API_Signature = GetValueConfig("api_signature", paypalStandardSettings);
            UseSandbox = GetValueConfig("use_sandbox", paypalStandardSettings);
        }

        public override IList<PHSettingItem> ToListAttribute()
        {
            var listAtrr = new List<PHSettingItem>();

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "API Username",
                Name = "api_username",
                TypeSetting = API_UserName.GetType(),
                ValueInStr = API_UserName.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "API Password",
                Name = "api_userpass",
                TypeSetting = API_Password.GetType(),
                ValueInStr = API_Password.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "Signature",
                Name = "api_signature",
                TypeSetting = API_Signature.GetType(),
                ValueInStr = API_Signature.ToString()
            });

            listAtrr.Add(new PHSettingItem()
            {
                DisplayName = "Use Sandbox",
                Name = "use_sandbox",
                TypeSetting = UseSandbox.GetType(),
                ValueInStr = UseSandbox.ToString()
            });

            return listAtrr;
        }
    }
}
